package andriod.action.mail;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import andriod.service.ArdMailService;
import andriod.util.StringProcessor;

import com.jcrj.action.BaseAction;
import com.jcrj.entity.ODraftMail;
import com.jcrj.entity.OFile;
import com.jcrj.entity.OInMail;
import com.jcrj.entity.OMailLog;
import com.jcrj.entity.OMailReceiver;
import com.jcrj.entity.OUser;
import com.jcrj.service.FileService;
import com.jcrj.service.MailService;
import com.jcrj.service.SysLogService;
import com.jcrj.util.Base64;
import com.jcrj.util.DateProcessor;
import com.jcrj.util.DateUtil;

public class ArdMailAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private ArdMailService ardMailService;
	private MailService mailService;
	private FileService fileService;
	private SysLogService sysLogService;
	private ODraftMail mail;
	private int mailId;
	private String[] mailIds;
	private String tag;
	
	private long pageCount;
	private long totalCount;
	private String title;
	private String name;
	private String startDate;
	private String endDate;
	
	public String mailHome() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			JSONObject obj = new JSONObject();
			long count = this.ardMailService.countDraftMail(user.getId().intValue());
			obj.put("draftCount", Long.valueOf(count));
			String userId = this.sysLogService.getAgentUser(user);
			count = this.ardMailService.countInMail(userId);
			obj.put("inCount", Long.valueOf(count));
			count = this.ardMailService.countSendMail(user.getId().intValue());
			obj.put("sendCount", Long.valueOf(count));
			count = this.ardMailService.countDeleteMail(user.getId().intValue());
			obj.put("deleteCount", Long.valueOf(count));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String mailWrite() {
		try {
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String saveGraftMail() {
		try {
			if ((this.mail.getStatus() == null) || ("".equals(this.mail.getStatus()))) {
				this.mail.setStatus("普通");
			}
			this.mail.setCreateTime(new Date());
			OUser user = (OUser) super.getSession().get("userInfo");
			this.mail.setSendId(user.getId());
			this.mail.setSendName(user.getName());
			this.mailService.saveDraftMail(this.mail);
			this.sysLogService.saveLog("邮件", user.getName(), "添加了" + this.mail.getTitle() + "邮件草稿");
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String saveInMail() {
		try {
			if ((this.mail.getStatus() == null) || ("".equals(this.mail.getStatus()))) {
				this.mail.setStatus("普通");
			}
			this.mail.setCreateTime(new Date());
			OUser user = (OUser) super.getSession().get("userInfo");
			this.mail.setSendId(user.getId());
			this.mail.setSendName(user.getName());
			this.mailService.saveInMail(this.mail, user, null);// =========================
			this.sysLogService.saveLog("邮件", user.getName(), "发送了一封" + this.mail.getTitle() + "的邮件");
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getDraftMailList() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			List list = this.ardMailService.getDraftMailList(user.getId().intValue());
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			JSONArray obj = JSONArray.fromObject(list, config);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getDraftMailUpdate() {
		try {
			ODraftMail mail = this.mailService.getDraftMail(this.mailId);
			List list = this.fileService.getFileList(mail.getFileId());
			JSONObject obj = new JSONObject();
			obj.put("object", mail);
			obj.put("fileList", list);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String deleteDraftMail() {
		try {
			this.mailService.deleteDraftMail(this.mailIds);
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getInMailList() {
		try {
			
			
			OUser user = (OUser) super.getSession().get("userInfo");
			String userId = this.sysLogService.getAgentUser(user);
			
//			List<OMailReceiver> list = this.ardMailService.getInMailList(userId);
			List<OMailReceiver> list = this.mailService.getInMailList(this.title, this.name, this.startDate, this.endDate, userId, super.getCurrentPage());
			for (OMailReceiver o : list) {
				o.getInMail().setContent(Base64.decode(o.getInMail().getContent()));
			}
			
			this.totalCount = this.mailService.getInMailCount(this.title, this.name, this.startDate, this.endDate, userId).longValue();
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
			
			JSONObject obj = new JSONObject();
			
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			JSONArray array = JSONArray.fromObject(list, config);
			
			obj.put("totalCount", this.totalCount);
			obj.put("pageCount", this.pageCount);
			obj.put("currentPage", super.getCurrentPage());
			obj.put("list", array);
			
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getOInMail() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			OInMail mail = this.mailService.updateGetInMail(this.mailId, user);
			if ((mail.getContent() != null) && (!"".equals(mail.getContent()))) {
				mail.setContent(Base64.decode(mail.getContent()));
			}
			List<OFile> list = this.fileService.getFileList(mail.getFileId());
			JSONObject obj = new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			DateProcessor dateProcessor = new DateProcessor();
			jsonConfig.registerJsonValueProcessor(Date.class, dateProcessor);
			jsonConfig.registerJsonValueProcessor(java.sql.Date.class, dateProcessor);
			JSONObject mailJson = JSONObject.fromObject(mail, jsonConfig);
			obj.put("object", mailJson);
			JSONArray fileListJson = JSONArray.fromObject(list, jsonConfig);
			obj.put("fileList", fileListJson);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String updateInMail() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			this.mailService.updateInMail(this.mailIds, user);
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String deleteInMail() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			this.mailService.deleteInMail(this.mailIds, user);
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String mailBackApprove() {
		try {
			OInMail mail = this.mailService.getInMailInfo(this.mailId);
			ODraftMail dmail = new ODraftMail();
			JSONObject obj = new JSONObject();
			if ("back".equals(this.tag)) {
				dmail.setTitle("Re:" + mail.getTitle());
				dmail.setReceiverIds(mail.getSendId().toString());
				dmail.setReceiverNames(mail.getSendName());
			} else {
				dmail.setTitle("Fw:" + mail.getTitle());
				dmail.setFileId(mail.getFileId());
				List list = this.fileService.getFileList(dmail.getFileId());
				obj.put("fileList", list);
			}
			String content = "-------邮件信息-------<br/>发件人：" + mail.getSendName() + "<br/>发送日期：" + DateUtil.DateToFormat(mail.getCreateTime()) + "<br/>收件人："
					+ mail.getReceiverName() + "<br/>标题：" + mail.getTitle();
			if ((mail.getContent() != null) && (!"".equals(mail.getContent()))) {
				content = content + "<br/>" + Base64.decode(mail.getContent());
			}
			dmail.setContent(content);
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(String.class, new StringProcessor());
			JSONObject dmailJson = JSONObject.fromObject(dmail, config);
			obj.put("object", dmailJson);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getSendMailList() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			List<OInMail> list = this.ardMailService.getSendMailList(user.getId().intValue());
			for (OInMail o : list) {
				o.setContent(Base64.decode(o.getContent()));
			}
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			JSONArray obj = JSONArray.fromObject(list, config);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getSendMailInfo() {
		try {
			OInMail mail = this.mailService.getInMailInfo(this.mailId);
			if ((mail.getContent() != null) && (!"".equals(mail.getContent()))) {
				mail.setContent(Base64.decode(mail.getContent()));
			}
			List list = this.fileService.getFileList(mail.getFileId());
			JSONObject obj = new JSONObject();
			obj.put("object", mail);
			obj.put("fileList", list);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String deleteSendMail() {
		try {
			this.mailService.deleteOuttMail(this.mailIds);
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String mailRollBack() {
		try {
			boolean flag = this.mailService.addMailRollBck(this.mailId);
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(flag));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getDeleteMailList() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			List<OMailReceiver> list = this.ardMailService.getDeleteMailList(user.getId().intValue());
			for (OMailReceiver o : list) {
				o.getInMail().setContent(Base64.decode(o.getInMail().getContent()));
			}

			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			JSONArray obj = JSONArray.fromObject(list, config);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String mailReadInfo() {
		try {
			List<OMailLog> readList = this.mailService.readUserList(this.mailId);
			List<String> notReadList = this.mailService.notReadUserList(this.mailService.getUserList(readList), this.mailId);
			JSONObject obj = new JSONObject();
			obj.put("readList", readList);
			obj.put("notReadList", notReadList);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public ArdMailService getArdMailService() {
		return this.ardMailService;
	}

	public void setArdMailService(ArdMailService ardMailService) {
		this.ardMailService = ardMailService;
	}

	public MailService getMailService() {
		return this.mailService;
	}

	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

	public FileService getFileService() {
		return this.fileService;
	}

	public void setFileService(FileService fileService) {
		this.fileService = fileService;
	}

	public SysLogService getSysLogService() {
		return this.sysLogService;
	}

	public void setSysLogService(SysLogService sysLogService) {
		this.sysLogService = sysLogService;
	}

	public ODraftMail getMail() {
		return this.mail;
	}

	public void setMail(ODraftMail mail) {
		this.mail = mail;
	}

	public int getMailId() {
		return this.mailId;
	}

	public void setMailId(int mailId) {
		this.mailId = mailId;
	}

	public String[] getMailIds() {
		return this.mailIds;
	}

	public void setMailIds(String[] mailIds) {
		this.mailIds = mailIds;
	}

	public String getTag() {
		return this.tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}
	
	public String getStartDate() {
		return this.startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return this.endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
}
